<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Session;
class Xj extends Controller
{
    public function index()
    {
       
        $xj = Db::table('preach')
        ->alias('a')
        ->join('company c','c.company_id = a.company_id')
        ->where('preach_status',0)
        ->order('preach_id desc')
        ->paginate(6);
        
        // dump($xj);
        // exit;
        $this->assign("xj",$xj);
        $page = $xj->render();
        // dump($page);
        // exit;
        $this->assign('page', $page);
        $gw1 = Db::table('position')
        ->alias('a')
        ->join('company c','c.company_id = a.company_id')
        ->where('position_status',0)
        ->order('position_id desc')
        ->limit(3)
    ->select();
        // dump($gw1);
        // exit;
        
        $this->assign("gw1",$gw1);
       return $this->fetch();
    }
    public function xjdetails($preach_id)
    {
       
        $xj = Db::table('preach')
        ->alias('a')
        ->join('company c','c.company_id = a.company_id')
        ->where('preach_id',$preach_id)
        ->where('preach_status',0)
        ->select();
       $this->assign("xj",$xj);
      return $this->fetch();
    }
    public function baoming($preach_id)
    {
        $user_key = Session::has('user_id');
        if(!$user_key)
        {
            return $this->error('请登录后，再操作！');
        }
        $user_id=Session::get('user_id');
       $data = [
        'preach_id' => $preach_id,
        'name' => input('name'),
        'school' => input('school'),
        'user_id' => $user_id,
       ];
       $b = Db::name('baoming')->where($data)->find();
        // dump($res);
        // exit;
        if($b==null){

            $res = Db::table('baoming')->insert($data);
                if($res)
                {
                    return $this->success('报名成功','xj/index');
                }
                else
                {
                    return $this->error('报名失败');
                }
            
        }else{
            return $this->error('已经报名过！');
        }
    }
}
